<!DOCTYPE html>
{% load static %}
<html>
<head>
    <meta charset="UTF-8">
    <title>找回密码</title>
    <link rel="icon" type="image/x-icon" href="{% static 'img/icon.ico' %}">
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f2f2f2;
            background-image: url("{% static 'img/myimg.jpg' %}");
            background-size: cover;
            background-position: center;
            height: 100vh;
            margin: 0;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .container {
            max-width: 400px;
            padding: 20px;
            background-color: #fff;
            background-color: rgba(255, 255, 255, 0.6);
            backdrop-filter: blur(10px);
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 10px;
        }
        input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button[type="submit"] {
            display: block;
            width: 100%;
            padding: 10px;
            {#margin-top: 20px;#}
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button[type="submit"]:hover {
            background-color: #007BFF; /* 设置悬浮时的颜色 */
        }
        #sendEmailCode {
            {#display: inline-block; /* 让按钮在同一行显示 */#}
            padding: 10px;
            background-color: #007BFF; /* 设置按钮颜色 */
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            {#margin-top: 10px; /* 设置按钮上边距 */#}
        }
        #sendEmailCode:hover {
            background-color: #0056b3; /* 设置悬浮时的颜色 */
        }

        #btn_sub {
            {#display: inline-block; /* 让按钮在同一行显示 */#}
            padding: 10px;
            background-color: #007BFF; /* 设置按钮颜色 */
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 10px; /* 设置按钮上边距 */
        }
        #btn_sub:hover {
            background-color: #0056b3; /* 设置悬浮时的颜色 */
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>找回密码</h1>
        <form method="post" action="{% url 'forget_password' %}" id='forget_password'>
            {% csrf_token %}
{#            <!-- {{ form.username.label_tag }}#}
{#            {{ form.username }}#}
{#            {{ form.email.label_tag }}#}
{#            {{ form.email }}#}
{#            {{ form.password.label_tag }}#}
{#            {{ form.password }}#}
{#            {{ form.form.email_code.label_tag }}#}
{#            {{ form.email_code }}#}
{#            <button type="submit">找回密码</button> -->#}

            <table>
                <tr>
                    <td class="td_left"><label for="username">
                        用户名<span class="Required-fields"> *</span>
                    </label></td>
                    <td class="td_right" colspan="2">
                        {{ form.username }}
{#                        <input type="username" name="username" id="username" placeholder="请输入用户名">#}
                    </td>
                </tr>


                <tr>
                    <td class="td_left"><label for="email">
                        Email<span class="Required-fields"> *</span>
                    </label></td>
                    <td class="td_right">
                        <input type="email" name="email" id="email" placeholder="请输入邮箱">
                    </td>
                    <td><button type="button" id="sendEmailCode">发送验证码</button></td>
                </tr>

                <tr>
                    <td class="td_left"><label for="email_code">
                        邮箱验证码
                        <span class="Required-fields"> *</span></label>
                    </td>
                    <td class="td_right" colspan="2">
                        <input type="text" name="email_code" id="email_code" placeholder="请输入邮箱验证码">
                    </td>
                </tr>

                <tr>
                    <td class="td_left"><label for="username">
                        新密码<span class="Required-fields"> *</span>
                    </label></td>
                    <td class="td_right" colspan="2">
                        {{ form.password }}
                    </td>
                </tr>

                <tr>
                    <td class="td_left"><label for="username">
                        重复新密码<span class="Required-fields"> *</span>
                    </label></td>
                    <td class="td_right" colspan="2">
                        {{ form.repassword }}
                    </td>
                </tr>

                <tr>
                    <td></td>
                    <td  align="center"><input id="btn_sub" type="submit" value="提交"></td>
                    <td></td>
                </tr>

            <!-- 错误信息模板位置 -->
            <p class="error">{{ error_message }}</p>
        </form>
    </div>
</body>

<script>
    document.getElementById('sendEmailCode').addEventListener('click', function() {
        console.log('捕获事件');

        // 获取用户填写的邮箱
        var userEmail = document.getElementById('email').value;
        var username = document.getElementById('username').value;

        // 进行邮箱格式检查
        if (!validateEmail(userEmail)) {
            alert('请输入有效的邮箱地址！');
            return;
        }

        // 调用后台发送邮件的函数
        sendEmailCode(username, userEmail);
    });

    function validateEmail(email) {
        console.log('邮箱检查');

        // 简单的邮箱格式检查
        var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        return emailRegex.test(email);
    }

    function sendEmailCode(username, email) {
        console.log('调用后台发送消息');

        var csrf_token = $('[name="csrfmiddlewaretoken"]').val();
        var sendEmailButton = $('#sendEmailCode');
        var countdown = 60;

        // 禁用按钮，并显示倒计时
        sendEmailButton.prop('disabled', true);

        function updateCountdown() {
            sendEmailButton.text('重新发送(' + countdown + 's)');
            countdown--;

            if (countdown < 0) {
                clearInterval(timer);
                sendEmailButton.text('发送验证码');
                sendEmailButton.prop('disabled', false);  // 启用按钮
            } else {
                // 继续更新倒计时
                timer = setInterval(updateCountdown, 1000);
            }
        }

        // 更新倒计时，并每秒执行一次
        var timer = setInterval(updateCountdown, 1000);

        $.ajax({
            url: '/user/forget_pwd_send_email_code/',
            type: 'POST',
            data: {
                'email': email,
                'username': username,  // 添加用户名到请求数据中
                'csrfmiddlewaretoken': csrf_token,
            },
            success: function(response) {
                // 处理后台返回的响应
                console.log('邮件验证码发送成功，后台响应：', response);
                {#alert('验证码已发送至您的邮箱，请查收！');#}
                if (response.status === 'success') {
                    alert('验证码已发送至您的邮箱，请查收！');
                } else {
                    alert('用户名与邮箱不匹配');
                    clearInterval(timer);  // 立即解除倒计时
                    sendEmailButton.text('发送验证码');
                    sendEmailButton.prop('disabled', false);  // 启用按钮
                }
            },
            error: function(error) {
                // 处理发送失败的情况
                console.error('发送验证码失败，错误信息：', error);
                alert('发送验证码失败，请稍后重试！如遇多次，请清除浏览器缓存或更换浏览器');
                clearInterval(timer);  // 立即解除倒计时
                sendEmailButton.text('发送验证码');
                sendEmailButton.prop('disabled', false);  // 启用按钮
            }
        });
    }
</script>
</html>